use lettre::message::header::ContentType;
use lettre::{Message, SmtpTransport, Transport};
use lettre::transport::smtp::authentication::Credentials;
use crate::config::info;
use crate::config::info::Master;


pub enum EmailOps{
    VerificationCode
}
pub struct EmailQueue{
    pub(crate) ops:EmailOps,
    pub(crate) body:String,
    pub(crate) to:String,
}

impl EmailQueue {
    pub fn new(info:Master) ->SmtpTransport{
        let creds = Credentials::new(
            info.email().addr.to_owned(),
            info.email().password.to_owned()
        );
        let mailer = SmtpTransport::relay(info.email().smtp.as_str())
            .unwrap()
            .credentials(creds)
            .build();
        mailer
    }
    fn send_code(&self,mailer:SmtpTransport,info:info::mail::Email){
        let email = Message::builder()
            .from(info.addr.parse().unwrap())
            .subject("验证码")
            .header(ContentType::TEXT_PLAIN)
            .to(self.to.parse().unwrap())
            .body(self.body.clone()).unwrap();
        mailer.send(&email).unwrap();
    }
    pub fn send(&self,mailer:SmtpTransport,info:info::mail::Email){
        match self.ops {
            EmailOps::VerificationCode => {
                self.send_code(mailer,info);
            }
        }
    }
}
